package com.tlias.controller;

import com.tlias.Service.EmpService;
import com.tlias.pojo.Emp;
import com.tlias.pojo.PageBean;
import com.tlias.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.io.ResolverUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {

    //依赖注入
    @Autowired
    private EmpService empService;

    /*
    * 分页查询
    * */
    @GetMapping
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String name, Short gender,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
        log.info("分页查询,参数：{},{},{},{},{},{}",page ,pageSize,name,gender,begin,end);
        //调用Service分页查询
        PageBean pageBean = empService.page(page,pageSize,name,gender,begin,end);
        //返回结果封装
        return Result.success(pageBean);
    }

    /*
    * 批量删除
    * */
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        log.info("批量删除员工：{}",ids);
        //调用Service批量删除
        empService.delete(ids);
        return Result.success();
    }

    /*
    * 新增员工
    * */
    @PostMapping
    public Result save(@RequestBody Emp emp){
        log.info("新增员工 emp：{}",emp);
        //调用Service新增员工
        empService.insert(emp);
        return Result.success();
    }

    /*
    * 根据ID查询员工
    * */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("根据ID查询,id:{}",id);
        //调用Service方法进行查询
        //将查询返回的数据封装到list集合中响应给前端
        Emp emp = empService.getById(id);
        return Result.success(emp);
    }

    /*
    * 更改员工数据
    * */
    @PutMapping
    public Result update(@RequestBody Emp emp){
        log.info("更改员工信息：{}",emp);
        //调用Service方法进行修改
        empService.update(emp);
        return Result.success();
    }



}
